<template>
  <div class="home">
    <div class="welcome-section">
      <h2>欢迎使用数据库测试应用</h2>
      <p>这是一个基于Vue.js和FastAPI开发的前后端分离应用</p>
    </div>
    
    <div class="actions" v-if="!isLoggedIn">
      <button @click="openLogin" class="btn login-btn">登录</button>
      <button @click="openRegister" class="btn register-btn">注册</button>
    </div>
    
    <div class="user-dashboard" v-else>
      <div class="user-info">
        <h3>欢迎, {{ currentUser.username }}!</h3>
        <p>用户ID: {{ currentUser.user_id }}</p>
        <div class="user-actions">
          <button @click="goToSections" class="btn sections-btn">板块列表</button>
          <button @click="goToProfile" class="btn profile-btn">个人信息管理</button>
          <button @click="logout" class="btn logout-btn">退出登录</button>
        </div>
      </div>
    </div>
    
    <div class="public-actions" v-if="!isLoggedIn">
      <button @click="goToSections" class="btn sections-btn">浏览板块</button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      isLoggedIn: false,
      currentUser: null
    }
  },
  mounted() {
    // 检查本地存储中的用户信息
    this.checkLoginStatus();
    
    // 监听登录成功事件
    this.$eventBus.on('login-success', (data) => {
      this.handleLoginSuccess(data);
    });
    
    // 监听登出事件
    this.$eventBus.on('user-logout', () => {
      this.isLoggedIn = false;
      this.currentUser = null;
    });
  },
  methods: {
    checkLoginStatus() {
      const user = localStorage.getItem('user');
      if (user) {
        this.currentUser = JSON.parse(user);
        this.isLoggedIn = true;
      }
    },
    handleLoginSuccess(data) {
      // 处理登录成功事件
      this.checkLoginStatus();
    },
    openLogin() {
      // 通过事件总线触发打开登录弹窗
      this.$eventBus.emit('open-login');
    },
    openRegister() {
      // 通过事件总线触发打开注册弹窗
      this.$eventBus.emit('open-register');
    },
    logout() {
      localStorage.removeItem('user');
      this.isLoggedIn = false;
      this.currentUser = null;
      // 触发登出事件
      this.$eventBus.emit('user-logout');
    },
    goToProfile() {
      // 导航到个人信息管理页面
      this.$router.push('/profile');
    },
    goToSections() {
      // 导航到板块列表页面
      this.$router.push('/sections');
    }
  },
  // 添加activated钩子，确保在路由切换时更新状态
  activated() {
    this.checkLoginStatus();
  }
}
</script>

<style scoped>
.home {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.welcome-section {
  text-align: center;
  margin-bottom: 40px;
}

.welcome-section h2 {
  color: #007bff;
  margin-bottom: 10px;
  font-size: 2rem;
}

.welcome-section p {
  color: #666;
  font-size: 1.1rem;
}

.actions {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-top: 30px;
}

.public-actions {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.btn {
  padding: 12px 24px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s;
}

.login-btn {
  background-color: #007bff;
  color: white;
}

.login-btn:hover {
  background-color: #0056b3;
}

.register-btn {
  background-color: #28a745;
  color: white;
}

.register-btn:hover {
  background-color: #218838;
}

.sections-btn {
  background-color: #6f42c1;
  color: white;
}

.sections-btn:hover {
  background-color: #5a32a3;
}

.user-dashboard {
  display: flex;
  justify-content: center;
  margin-top: 30px;
}

.user-info {
  background-color: #f8f9fa;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  text-align: center;
  max-width: 400px;
  width: 100%;
}

.user-info h3 {
  color: #007bff;
  margin-bottom: 10px;
}

.user-info p {
  color: #666;
  margin-bottom: 20px;
}

.user-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.profile-btn {
  background-color: #6f42c1;
  color: white;
}

.profile-btn:hover {
  background-color: #5a32a3;
}

.logout-btn {
  background-color: #dc3545;
  color: white;
}

.logout-btn:hover {
  background-color: #c82333;
}

@media (max-width: 768px) {
  .home {
    padding: 10px;
  }
  
  .actions {
    flex-direction: column;
    align-items: center;
  }
  
  .actions .btn {
    width: 80%;
  }
}
</style>